package com.example.jack.community.view.me.general;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jack.community.MainActivity;
import com.example.jack.community.R;
import com.example.jack.community.data.me.Brand;
import com.example.jack.community.view.me.BlackStatusActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 多语言选项
 */
public class LanActivity extends BlackStatusActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.txt_back)
    TextView txtBack;
    @BindView(R.id.finish_text)
    TextView finishText;
    private ListView mListView; //首页的ListView
    private List<Brand> namesList; //用于装载数据的集合
    private int selectPosition = -1;//用于记录用户选择的变量
    private Brand select_langusgu; //用户选择的品牌

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lan2);
        ButterKnife.bind(this);
        initView();
        initDatas();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.id_myList);
    }

    private void initDatas() {
        //初始化ListView适配器的数据
        namesList = new ArrayList<>();
        Brand brand0 = new Brand("简体中文");
        Brand brand1 = new Brand("English");
        namesList.add(brand0);
        namesList.add(brand1);
        final MyAdapter myAdapter = new MyAdapter(this, namesList);
        mListView.setAdapter(myAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取选中的参数
                selectPosition = position;
                myAdapter.notifyDataSetChanged();
                select_langusgu = namesList.get(position);

                if (select_langusgu.getBandname().equals("English")) {
                    Resources resources = getResources();
                    DisplayMetrics dm = resources.getDisplayMetrics();
                    Configuration config = resources.getConfiguration();
                    // 应用用户选择语言
                    config.locale = Locale.ENGLISH;
                    resources.updateConfiguration(config, dm);
                    SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                    editor.putString("language", "en");
                    editor.apply();
                }
                if (select_langusgu.getBandname().equals("简体中文")) {
                    Resources resources = getResources();
                    DisplayMetrics dm = resources.getDisplayMetrics();
                    Configuration config = resources.getConfiguration();
                    // 应用用户选择语言
                    config.locale = Locale.TRADITIONAL_CHINESE;
                    resources.updateConfiguration(config, dm);
                    SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                    editor.putString("language", "zh");
                    editor.apply();
                }
                Toast.makeText(LanActivity.this, "您选中的语言是：" + select_langusgu.getBandname(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick({R.id.img_back, R.id.txt_back, R.id.finish_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.txt_back:
                finish();
                break;
            case R.id.finish_text:
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                break;
        }
    }

    public class MyAdapter extends BaseAdapter {
        Context context;
        List<Brand> brandsList;
        LayoutInflater mInflater;

        public MyAdapter(Context context, List<Brand> mList) {
            this.context = context;
            this.brandsList = mList;
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return brandsList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.me_lan_item, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.name = (TextView) convertView.findViewById(R.id.id_name);
                viewHolder.select = (RadioButton) convertView.findViewById(R.id.id_select);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.name.setText(brandsList.get(position).getBandname());

            if (selectPosition == position) {
                viewHolder.select.setChecked(true);
            } else {
                viewHolder.select.setChecked(false);
            }
            return convertView;
        }
    }


    public class ViewHolder {
        TextView name;
        RadioButton select;
    }
}
